import { IPos } from "./model"
import { moveJArgs, MoveJ, MoveL, 
    moveGripper,
    SetTPDStart, SetWebTPDStop, MoveTPD, MoveGripper, ActGripper,
    moveLArgs, RESETALLERROR, 
    setTPDStartArgs, 
    setWebTPDStopArgs, 
    moveTPDArgs } from "./proto"

import { robot, waitRobotReady, waitGripperReady } from "./robot"
import { changeState } from "./exec"

export default {

    async setTPDStart(
        filename: string,
        msPeriod: number = 2
    ) {
        changeState(true, "示教开始");
        
        await robot.request(
            SetTPDStart.cmdCode,
            SetTPDStart.name,
            setTPDStartArgs(filename, msPeriod)
        );
    },

    async setWebTPDStop(filename: string, msPeriod: number = 2) {
        changeState(true, "示教结束");
        await robot.request(
            SetWebTPDStop.cmdCode,
            SetWebTPDStop.name,
            setWebTPDStopArgs(filename, msPeriod)
        );
    },

    async moveTPD(
        filename: string,
        bland: number = 1,
        ovl: number = 200,
        timeout: number = 10 * 1000,
    ) {
        await robot.request(
            MoveTPD.cmdCode,
            MoveTPD.name,
            moveTPDArgs(filename, bland, ovl)
        );
        await waitRobotReady(timeout);
    },

    async moveGripper(
        pos: number,
        speed: number = 100,
        tor: number = 100,
        maxTime: number = 30 * 1000,
        timeout: number = 10 * 1000,
    ) {
        await robot.request(
            MoveGripper.cmdCode,
            MoveGripper.name,
            moveGripper(pos, speed, tor, maxTime)
        );
        await waitGripperReady(timeout);
    },

    async resetAllError() {
        await robot.request(
            RESETALLERROR.cmdCode,
            RESETALLERROR.name,
            []
        );
    },

    async moveJs(
        posList: IPos[],
        speed: number = 100, 
        acc: number = 180, 
        ovl: number = 100, 
        timeout: number = 10 * 1000,
    ) {
        for (let index = 0; index < posList.length; index ++) {
            let pos = posList[index];
            let bland = 100;
            if (index == (posList.length - 1)) {
                bland = 0;
            }
            await robot.request(
                MoveJ.cmdCode,
                MoveJ.name,
                moveJArgs(pos.jt, pos.tl, speed, acc, ovl, bland)
            );
        }
        await waitRobotReady(timeout * posList.length);
    },

    async moveJ(
        pos: IPos,
        speed: number = 100, 
        acc: number = 180, 
        ovl: number = 100, 
        blandT: number = 0,
        timeout: number = 10 * 1000,
    ) {
        await robot.request(
            MoveJ.cmdCode,
            MoveJ.name,
            moveJArgs(pos.jt, pos.tl, speed, acc, ovl, blandT)
        );
        await waitRobotReady(timeout);
    },

    async moveL(
        pos: IPos,
        speed: number = 100, 
        acc: number = 180, 
        ovl: number = 100, 
        blandT: number = 0,
        timeout: number = 10 * 1000,
    ) {
        await robot.request(
            MoveL.cmdCode,
            MoveL.name,
            moveLArgs(pos.jt, pos.tl, speed, acc, ovl, blandT)
        );
        await waitRobotReady(timeout);
    },
}
